<?php
class MY_Pagination extends CI_Pagination{

	var $js_function = '';
	var $next_link = 'Next ››';
	var $prev_link = '‹‹ Prev';
	var $show_total = 'no';
	var $show_text_total = 'of';
	var $show_num = 'yes';
	var $show_button = 'yes';
	var $show_first_last = 'yes';
	var $next_class = 'prevnext';
	var $prev_class = 'prevnext ';
	var $num_class = 'number';
	var $cur_class = 'currentpage';
	var $title_link =	'Page';
	var $total_tag_open = '';
	var $total_tag_close = '';
	var $cur_tag_open = '<li>';
	var $cur_tag_close = '</li>';	
	var $num_tag_open = '<li>';
	var $num_tag_close = '</li>';
	var $full_tag_open = '<ul>';
	var $full_tag_close = '</ul>';
	var $ext	=	'/';

	function display_ajax(){
		$pagination = '';
		if($this->total_rows > 1){
			$first = 0;
			$end = 0;
			if ($this->cur_page != 1) 
			{
				$first = $this->cur_page - 3;
				if ($first<=0) {$first = 1;$end = 4 -  $this->cur_page;}
				$end = $end + $this->cur_page + 3;
				if ($end > $this->total_rows) 
				{
					$end = $this->total_rows;
					$first = $this->total_rows - 6;
					if ($first <= 0) {$first = 1;}
				}
			}
			else {
				$first = 1;$end = 7;
				if ($end > $this->total_rows) {$end = $this->total_rows;}
			}
			
			$pagination.= $this->full_tag_open;
				if($this->show_button == 'yes'){
					if($this->cur_page>1)
					{
						if($this->show_first_last == 'yes'){
							$pagination .=	$this->first_tag_open;
							$pagination .= "<a href='javascript:void(0)' onclick='".$this->js_function."(1".")'>".$this->first_link."</a>";
							$pagination .=	$this->first_tag_close;
						}
						
						$pagination .=	$this->prev_tag_open;
						$pagination .= "<a href='javascript:void(0)' class='".$this->prev_class."' onclick='".$this->js_function."(".($this->cur_page-1).")'>".$this->prev_link."</a>";
						$pagination .=	$this->prev_tag_close;
					}
				}
				
				if($this->show_num == 'yes'){
					for($i = $first; $i <= $end; $i++)
					{
						if($i==$this->cur_page)
						{
							$pagination .= $this->num_tag_open;
							$pagination .= '<b>'.$i.'</b>';
							$pagination .= $this->num_tag_close;
						}
						else
						{
							$pagination .= $this->num_tag_open;
							$pagination .= "<a href='javascript:void(0)' class='number' onclick='".$this->js_function."(".$i.")'>".$i."</a>";
							$pagination .= $this->num_tag_close;
						}
					}
				}
				
				if($this->show_total == 'yes') $pagination .= '<span> '.$this->show_text_total.'&nbsp;'.$this->total_rows.'</span>';
				
				if($this->show_button == 'yes'){
					if($this->cur_page<$this->total_rows)
					{
						
							$pagination.= $this->next_tag_open;
							$pagination.=	"<a href='javascript:void(0)' class='".$this->next_class."' onclick='".$this->js_function."(".($this->cur_page+1).")'>".$this->next_link."</a>";
							$pagination.= $this->next_tag_close;
						
						if($this->show_first_last == 'yes'){
							$pagination.=	$this->last_tag_open;
							$pagination.= "<a href='javascript:void(0)' onclick='".$this->js_function."(".$this->total_rows.")'>".$this->last_link."</a>";
							$pagination.=	$this->last_tag_close;
						}
					}
				}
				
			$pagination.= $this->full_tag_close;
		}	
		return $pagination;
	}
	
	function ajax_url(){
		$pagination = '';
		if($this->total_rows > 1){
			$first = 0;
			$end = 0;
			if ($this->cur_page != 1) 
			{
				$first = $this->cur_page - 3;
				if ($first<=0) {$first = 1;$end = 4 -  $this->cur_page;}
				$end = $end + $this->cur_page + 3;
				if ($end > $this->total_rows) 
				{
					$end = $this->total_rows;
					$first = $this->total_rows - 6;
					if ($first <= 0) {$first = 1;}
				}
			}
			else {
				$first = 1;$end = 7;
				if ($end > $this->total_rows) {$end = $this->total_rows;}
			}
			
			$pagination.= $this->full_tag_open;
				
				if($this->cur_page>1)
				{
					$pagination .=	$this->first_tag_open;
					$pagination .= "<a href='javascript:void(0)' onclick=".$this->js_function."('".$this->base_url."')>".$this->first_link."</a>";
					$pagination .=	$this->first_tag_close;
					
					$pagination .=	$this->prev_tag_open;
					$pagination .= "<a href='javascript:void(0)' onclick=".$this->js_function."('".$this->base_url."/".($this->cur_page-1)."')>".$this->prev_link."</a>";
					$pagination .=	$this->prev_tag_close;
				}
				
				for($i = $first; $i <= $end; $i++)
				{
					
					if($i==$this->cur_page)
					{
						$pagination .= $this->num_tag_open;
						$pagination .= '<a class="currentpage">'.$i.'</a>';
						$pagination .= $this->num_tag_close;
					}
					else
					{
						$pagination .= $this->num_tag_open;
						$pagination .= "<a href='javascript:void(0)' class='number' onclick=".$this->js_function."('".$this->base_url."/".$i."')>".$i."</a>";
						$pagination .= $this->num_tag_close;
					}
					
				}
				
				if($this->show_total == 'yes') $pagination .= '<span> '.$this->show_text_total.'&nbsp;'.$this->total_rows.'</span>';
				
				if($this->cur_page<$this->total_rows)
				{
					$pagination.= $this->next_tag_open;
					$pagination.=	"<a href='javascript:void(0)' onclick=".$this->js_function."('".$this->base_url."/".($this->cur_page+1)."')>".$this->next_link."</a>";
					$pagination.= $this->next_tag_close;
					
					$pagination.=	$this->last_tag_open;
					$pagination.= "<a href='javascript:void(0)' onclick=".$this->js_function."('".$this->base_url."/".$this->total_rows."')>".$this->last_link."</a>";
					$pagination.=	$this->last_tag_close;
				}
				
			$pagination.= $this->full_tag_close;
		}	
		return $pagination;
	}
	
	function display_ajax2(){
		$first = 0;
		$end = 0;
		if ($this->cur_page != 1) 
		{
			$first = $this->cur_page - 3;
			if ($first<=0) {$first = 1;$end = 4 -  $this->cur_page;}
			$end = $end + $this->cur_page + 3;
			if ($end > $this->total_rows) 
			{
				$end = $this->total_rows;
				$first = $this->total_rows - 6;
				if ($first <= 0) {$first = 1;}
			}
		}
		else {
			$first = 1;$end = 7;
			if ($end > $this->total_rows) {$end = $this->total_rows;}
		}
		
		$pagination = '';
		
		$pagination.= $this->full_tag_open;
			
			if($this->cur_page>1)
			{
				$pagination .=	$this->first_tag_open;
				$pagination .= "<a href='javascript:void(0)' onclick=".$this->js_function."('".$this->base_url."/1'".")>".$this->first_link."</a>";
				$pagination .=	$this->first_tag_close;
				
				$pagination .=	$this->prev_tag_open;
				$pagination .= "<a href='javascript:void(0)' onclick=".$this->js_function."('".$this->base_url."/".($this->cur_page-1)."')>".$this->prev_link."</a>";
				$pagination .=	$this->prev_tag_close;
			}
			else
			{
				$pagination.=	$this->first_tag_open;
				$pagination.=	'<span>'.$this->first_link.'</span>';
				$pagination.=	$this->first_tag_close;
				
				$pagination.=	$this->prev_tag_open;
				$pagination.=	'<span>'.$this->prev_link.'</span>';
				$pagination.=	$this->prev_tag_close;
				
			}
			
			for($i = $first; $i <= $end; $i++)
			{
				
				if($i==$this->cur_page)
				{
					$pagination .= $this->num_tag_open;
					$pagination .= '<a id="current" class="number">'.$i.'</a>';
					$pagination .= $this->num_tag_close;
				}
				else
				{
					$pagination .= $this->num_tag_open;
					$pagination .= "<a href='javascript:void(0)' class='number' onclick=".$this->js_function."('".$this->base_url."/".$i."')>".$i."</a>";
					$pagination .= $this->num_tag_close;
				}
				
			}
			if($this->cur_page<$this->total_rows)
			{
				$pagination.= $this->next_tag_open;
				$pagination.=	"<a href='javascript:void(0)' onclick=".$this->js_function."('".$this->base_url."/".($this->cur_page+1)."')>".$this->next_link."</a>";
				$pagination.= $this->next_tag_close;
				
				$pagination.=	$this->last_tag_open;
				$pagination.= "<a href='javascript:void(0)' onclick=".$this->js_function."('".$this->base_url."/".$this->total_rows."')>".$this->last_link."</a>";
				$pagination.=	$this->last_tag_close;
			}
			else
			{
				$pagination .= $this->next_tag_open;
				$pagination .= '<span>'.$this->next_link.'</span>';
				$pagination .= $this->next_tag_close;
				
				$pagination .=	$this->last_tag_open;
				$pagination .=	'<span>'.$this->last_link.'</span>';
				$pagination .=	$this->last_tag_close;
			}
			
		$pagination.= $this->full_tag_close;	
		return $pagination;
	}
	
	function display(){
		$first = 0;
		$end = 0;
		if ($this->cur_page != 1) 
		{
			$first = $this->cur_page - 3;
			if ($first<=0) {$first = 1;$end = 4 -  $this->cur_page;}
			$end = $end + $this->cur_page + 3;
			if ($end > $this->total_rows) 
			{
				$end = $this->total_rows;
				$first = $this->total_rows - 6;
				if ($first <= 0) {$first = 1;}
			}
		}
		else {
			$first = 1;$end = 7;
			if ($end > $this->total_rows) {$end = $this->total_rows;}
		}
		
		$pagination = '';
		
		$pagination.= $this->full_tag_open;
			
			if($this->cur_page>1)
			{
				$pagination .=	$this->first_tag_open;
				$pagination .= "<a href='".$this->base_url."/1"."'>".$this->first_link."</a>";
				$pagination .=	$this->first_tag_close;
				
				$pagination .=	$this->prev_tag_open;
				$pagination .= "<a href='".$this->base_url."/".($this->cur_page-1)."'>".$this->prev_link."</a>";
				$pagination .=	$this->prev_tag_close;
			}
			else
			{
				$pagination.=	$this->first_tag_open;
				$pagination.=	'<span>'.$this->first_link.'</span>';
				$pagination.=	$this->first_tag_close;
				
				$pagination.=	$this->prev_tag_open;
				$pagination.=	'<span>'.$this->prev_link.'</span>';
				$pagination.=	$this->prev_tag_close;
				
			}
			
			for($i = $first; $i <= $end; $i++)
			{
				
				if($i==$this->cur_page)
				{
					$pagination .= $this->num_tag_open;
					$pagination .= '<a id="current" class="number">'.$i.'</a>';
					$pagination .= $this->num_tag_close;
				}
				else
				{
					$pagination .= $this->num_tag_open;
					$pagination .= "<a class='number' href='".$this->base_url."/".$i."'>".$i."</a>";
					$pagination .= $this->num_tag_close;
				}
				
			}
			if($this->cur_page<$this->total_rows)
			{
				$pagination.= $this->next_tag_open;
				$pagination.=	"<a href='".$this->base_url."/".($this->cur_page+1)."'>".$this->next_link."</a>";
				$pagination.= $this->next_tag_close;
				
				$pagination.=	$this->last_tag_open;
				$pagination.= "<a href='".$this->base_url."/".$this->total_rows."'>".$this->last_link."</a>";
				$pagination.=	$this->last_tag_close;
			}
			else
			{
				$pagination .= $this->next_tag_open;
				$pagination .= '<span>'.$this->next_link.'</span>';
				$pagination .= $this->next_tag_close;
				
				$pagination .=	$this->last_tag_open;
				$pagination .=	'<span>'.$this->last_link.'</span>';
				$pagination .=	$this->last_tag_close;
			}
			
			if($this->show_total == 'yes') $pagination .= '<span>&nbsp;of '.$this->total_rows.'</span>';
			
		$pagination.= $this->full_tag_close;	
		return $pagination;
	}
	
	function display_query_string(){
		$first = 0;
		$end = 0;
		if ($this->cur_page != 1) 
		{
			$first = $this->cur_page - 3;
			if ($first<=0) {$first = 1;$end = 4 -  $this->cur_page;}
			$end = $end + $this->cur_page + 3;
			if ($end > $this->total_rows) 
			{
				$end = $this->total_rows;
				$first = $this->total_rows - 6;
				if ($first <= 0) {$first = 1;}
			}
		}
		else {
			$first = 1;$end = 7;
			if ($end > $this->total_rows) {$end = $this->total_rows;}
		}
		
		$pagination = '';
		
		$pagination.= $this->full_tag_open;
			
			if($this->cur_page>1)
			{
				$pagination .=	$this->prev_tag_open;
				$pagination .= "<a href='".$this->base_url."&page=".($this->cur_page-1)."'>".$this->prev_link."</a>";
				$pagination .=	$this->prev_tag_close;
			}
			else
			{
				$pagination.=	$this->first_tag_open;
				$pagination.=	'<span>'.$this->first_link.'</span>';
				$pagination.=	$this->first_tag_close;
				
				$pagination.=	$this->prev_tag_open;
				$pagination.=	'<span>'.$this->prev_link.'</span>';
				$pagination.=	$this->prev_tag_close;
				
			}
			
			for($i = $first; $i <= $end; $i++)
			{
				
				if($i==$this->cur_page)
				{
					$pagination .= $this->num_tag_open;
					$pagination .= '<a id="current" class="currentpage">'.$i.'</a>';
					$pagination .= $this->num_tag_close;
				}
				else
				{
					$pagination .= $this->num_tag_open;
					$pagination .= "<a class='number' href='".$this->base_url."&page=".$i."'>".$i."</a>";
					$pagination .= $this->num_tag_close;
				}
				
			}
			if($this->cur_page<$this->total_rows)
			{
				$pagination.= $this->next_tag_open;
				$pagination.=	"<a href='".$this->base_url."&page=".($this->cur_page+1)."'>".$this->next_link."</a>";
				$pagination.= $this->next_tag_close;
				
				$pagination.=	$this->last_tag_open;
				$pagination.= "<a href='".$this->base_url."&page=".$this->total_rows."'>".$this->last_link."</a>";
				$pagination.=	$this->last_tag_close;
			}
			else
			{
				$pagination .= $this->next_tag_open;
				$pagination .= '<span>'.$this->next_link.'</span>';
				$pagination .= $this->next_tag_close;
			}
			
			if($this->show_total == 'yes') $pagination .= '<span>&nbsp;of '.$this->total_rows.'</span>';
			
		$pagination.= $this->full_tag_close;	
		return $pagination;
	}
	
	function non_ajax(){
		$pagination = '';
		$total = $this->total_rows;
		if($total > 1){
			$first = 0;
			$end = 0;
			if ($this->cur_page != 1) 
			{
				$first = $this->cur_page - 3;
				if ($first<=0) {$first = 1;$end = 4 -  $this->cur_page;}
				$end = $end + $this->cur_page + 3;
				if ($end > $this->total_rows) 
				{
					$end = $this->total_rows;
					$first = $this->total_rows - 6;
					if ($first <= 0) {$first = 1;}
				}
			}
			else {
				$first = 1;$end = 7;
				if ($end > $this->total_rows) {$end = $this->total_rows;}
			}
			
			$pagination.= $this->full_tag_open;
				
				if($this->cur_page>1)
				{
					if($this->show_first_last == 'yes'){
						$pagination .=	$this->first_tag_open;
						$pagination .= "<a href='".$this->base_url."/"."'>".$this->first_link."</a>";
						$pagination .=	$this->first_tag_close;
					}
					
					$pagination .=	$this->prev_tag_open;
					if(($this->cur_page-1)==1)
						$pagination .= "<a class='".$this->prev_class."' href='".$this->base_url."/"."'>".$this->prev_link."</a>";
					else
						$pagination .= "<a class='".$this->prev_class."' href='".$this->base_url."/". ($this->cur_page-1) ."/'>".$this->prev_link."</a>";
					$pagination .=	$this->prev_tag_close;
				}
				
				if($this->show_num == 'yes'){
					$j=0;
					for($i = $first; $i <= $end; $i++)
					{
						$j++;
						if($i==$this->cur_page)
						{
							$pagination .= $this->cur_tag_open;
							$pagination .= '<a class="'.$this->cur_class.'">'.$i.'</a>';
							$pagination .= $this->cur_tag_close;
						}
						else
						{
							$pagination .= $this->num_tag_open;
							if($i==1)
								$pagination .= "<a title='".$this->title_link.' '.$i."' class='".$this->num_class.(($j==7) ? ' end' : '')."' href='".$this->base_url."/"."'>".$i."</a>";
							else
								$pagination .= "<a title='".$this->title_link.' '.$i."' class='".$this->num_class.(($j==7) ? ' end' : '')."' href='".$this->base_url."/".$i."/'>".$i."</a>";
							$pagination .= $this->num_tag_close;
						}
						
					}
				}else{
					if($this->cur_page > 1 && $this->cur_page < $this->total_rows)
						$pagination .= '&nbsp;';
				}
				
				if($this->show_total == 'yes') $pagination .= $this->total_tag_open.'of '.$this->total_rows.$this->total_tag_close;
				
				if($this->cur_page<$this->total_rows)
				{
					$pagination.= $this->next_tag_open;
					$pagination.=	"<a class='".$this->next_class."' href='".$this->base_url."/".($this->cur_page+1)."/'>".$this->next_link."</a>";
					$pagination.= $this->next_tag_close;
					
					if($this->show_first_last == 'yes'){
						$pagination.=	$this->last_tag_open;
						$pagination.= "<a href='".$this->base_url."/".$this->total_rows."/'>".$this->last_link."</a>";
						$pagination.=	$this->last_tag_close;
					}
				}
				
				
			$pagination.= $this->full_tag_close;
		}	
		return $pagination;
	}
	
	function custom(){
		$pagination = '';
		$total = $this->total_rows;
		if($total > 1){
			$first = 0;
			$end = 0;
			if ($this->cur_page != 1) 
			{
				$first = $this->cur_page - 3;
				if ($first<=0) {$first = 1;$end = 4 -  $this->cur_page;}
				$end = $end + $this->cur_page + 3;
				if ($end > $this->total_rows) 
				{
					$end = $this->total_rows;
					$first = $this->total_rows - 6;
					if ($first <= 0) {$first = 1;}
				}
			}
			else {
				$first = 1;$end = 7;
				if ($end > $this->total_rows) {$end = $this->total_rows;}
			}
			
			$pagination.= $this->full_tag_open;
				
				if($this->cur_page>1)
				{
					if($this->show_first_last == 'yes'){
						$pagination .=	$this->first_tag_open;
						$pagination .= "<a href='".$this->base_url.$this->ext.""."'>".$this->first_link."</a>";
						$pagination .=	$this->first_tag_close;
					}
					
					$pagination .=	$this->prev_tag_open;
					if(($this->cur_page-1)==1)
						$pagination .= "<a class='".$this->prev_class."' href='".$this->base_url.$this->ext.""."'>".$this->prev_link."</a>";
					else
						$pagination .= "<a class='".$this->prev_class."' href='".$this->base_url."_". ($this->cur_page-1).$this->ext."'>".$this->prev_link."</a>";
					$pagination .=	$this->prev_tag_close;
				}
				
				if($this->show_num == 'yes'){
					$j=0;
					for($i = $first; $i <= $end; $i++)
					{
						$j++;
						if($i==$this->cur_page)
						{
							$pagination .= $this->cur_tag_open;
							$pagination .= '<a class="'.$this->cur_class.'">'.$i.'</a>';
							$pagination .= $this->cur_tag_close;
						}
						else
						{
							$pagination .= $this->num_tag_open;
							if($i==1)
								$pagination .= "<a title='".$this->title_link.' '.$i."' class='".$this->num_class.(($j==7) ? ' end' : '')."' href='".$this->base_url.$this->ext."'>".$i."</a>";
							else
								$pagination .= "<a title='".$this->title_link.' '.$i."' class='".$this->num_class.(($j==7) ? ' end' : '')."' href='".$this->base_url."_".$i.$this->ext."'>".$i."</a>";
							$pagination .= $this->num_tag_close;
						}
						
					}
				}else{
					if($this->cur_page > 1 && $this->cur_page < $this->total_rows)
						$pagination .= '&nbsp;';
				}
				
				if($this->show_total == 'yes') $pagination .= $this->total_tag_open.'of '.$this->total_rows.$this->total_tag_close;
				
				if($this->cur_page<$this->total_rows)
				{
					$pagination.= $this->next_tag_open;
					$pagination.=	"<a class='".$this->next_class."' href='".$this->base_url."_".($this->cur_page+1).$this->ext."'>".$this->next_link."</a>";
					$pagination.= $this->next_tag_close;
					
					if($this->show_first_last == 'yes'){
						$pagination.=	$this->last_tag_open;
						$pagination.= "<a href='".$this->base_url."_".$this->total_rows.$this->ext."'>".$this->last_link."</a>";
						$pagination.=	$this->last_tag_close;
					}
				}
				
				
			$pagination.= $this->full_tag_close;
		}	
		return $pagination;
	}
}
